package com.atguigu.juc.study.chapter14.rwlock;

import java.util.concurrent.TimeUnit;

/**
 * @author : leimingchao
 * @date Date : 2022年06月22日 6:39
 */
public class ReentrantReadWriteLockDemo2 {

    public static void main(String[] args) {

        MyResource myResource = new MyResource();

        for (int i = 1; i <= 10; i++) {

            int finalI = i;
            new Thread(() -> {
                myResource.writeByRw(finalI + "", finalI + "");
            }, String.valueOf(i)).start();
        }


        for (int i = 1; i <= 10; i++) {

            int finalI = i;
            new Thread(() -> {
                myResource.readByRw(finalI + "");
            }, String.valueOf(i)).start();
        }

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 1; i <= 3; i++) {
            int finalI = i;
            new Thread(() -> {
                myResource.writeByRw(finalI + "", finalI + "");
            }, "新写锁线程->" +String.valueOf(i)).start();
        }
    }
}
